const app=getApp();
var time = require('../../utils/util.js');
Page({
  data: {
    arrayList:[],
    n:'',
    imgList:[],
    display:'display:block',
    // display1:'display:none'
  },

  clickimage:function(e){
    var arr=[];
    arr.push(e.target.dataset.myimg);
    console.log(arr);
    wx.previewImage({
      urls:arr
    })
  },

  push(){
    let token=wx.getStorageSync('token');
    if(!token){
      wx.showModal({
        title:'提示',
        content:'请登录',
        success:function(res){
          if(res.confirm){
            console.log("点击了确定");
            wx.switchTab({
              url: '/pages/getuserinfo/getuserinfo',
            })
          }else if(res.cancel){
            console.log("点击取消")
            return false;
          }
        }
      })
    }else{
      wx.navigateTo({
        url: '/pages/upload/upload',
      })
    }
  },

  onLoad: function (options) {
    let token=wx.getStorageSync('token');
    if(!token){
        wx.showModal({
          title:'提示',
          content:'请登录',
          success:function(res){
            if(res.confirm){
              console.log("点击了确定");
              wx.switchTab({
                url: '/pages/getuserinfo/getuserinfo',
              })
            }else if(res.cancel){
              console.log("点击取消")
              return false;
            }
          }
        })
    }
    wx.request({
      url:`${app.globalData.url}/user/square`,
      dataType:'json',
      method:'GET',
      header:{
        "content-type":'application/json',
        authorization: wx.getStorageSync('token')
      },
      success:res=>{
        var arr=[];
        var arr1=[];
        var length=res.data.length;
        var name=wx.getStorageSync('userInfo').nickName;
        for(var i=0;i<length;i++){
            if(res.data[i].user_name==name){
                var date = new Date(parseInt(res.data[i].posts_time))
                res.data[i].posts_time=time.formatTime(date);
                arr.push(res.data[i]);
                if(res.data[i].posts_image==null){
                   arr1=[];
                }else{
                    console.log(res.data[i].posts_image);
                    res.data[i].posts_image= res.data[i].posts_image.split(',').filter(p=>p.length>0);
                    arr1.push(res.data[i].posts_image);
                }
            }
        }
        this.setData({
            arrayList:arr,
            imgList:arr1
        })
        if(this.data.arrayList.length!=0){
            this.setData({
                display:'display:none',
            })
        }
        var n=this.data.arrayList.length;
        this.setData({n:n})   
      },
      fail(){
        console.log("no");
      }
    })
   
  },
  onPullDownRefresh: function () {
    this.onLoad();
    this.setData({
      page:1
    });
    },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    wx.showToast({
      title: '已经没有更多数据了',
      icon:'none',
      duration:1500,
      mask:true
    });
  },
})